﻿namespace WGController32_CSharp
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Globalization;
    using System.Net;
    using System.Net.Sockets;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;
    using WG3000_COMM.Common;
    using WG3000_COMM.Core;

    public class FormCloudServer : Form
    {
        private ArrayList arrControllerSN = new ArrayList();
        private ArrayList arrRecordIndex = new ArrayList();
        private bool bDisplayDetail = true;
        private bool bStopWatchServer = true;
        private Button btnAdjustTime;
        private Button btnClear;
        private Button btnControllerInfo;
        private Button btnGetAllSwipes;
        private Button btnOpenFloor;
        private Button btnRemoteOpenDoor1;
        private Button btnSendCommand;
        private Button btnUploadAllPrivileges;
        private Button btnUploadPrivilegeSingle;
        private Button button2;
        private Button button4;
        private ComboBox cboDoors;
        private ComboBox cboFloors;
        private CheckBox checkBox1;
        private CheckBox checkBox2;
        private CheckBox chkAllControllerB;
        private CheckBox chkAllControllers;
        private CheckBox chkDisplayIP;
        private CheckBox chkGetAllSwipes;
        private CheckBox chkLogDetail;
        private ColumnHeader columnHeader1;
        private ColumnHeader columnHeader2;
        private ColumnHeader columnHeader3;
        private ColumnHeader columnHeader4;
        private IContainer components;
        public DateTimePicker dateEndHMS1;
        public DateTimePicker dtpActivate;
        public DateTimePicker dtpDeactivate;
        private GroupBox groupBox1;
        private GroupBox groupBox2;
        private GroupBox groupBox3;
        private GroupBox groupBox4;
        private Label label1;
        private Label label10;
        private Label label2;
        private Label label3;
        private Label label4;
        private Label label5;
        private Label label6;
        private Label label7;
        private Label label8;
        private Label label9;
        private ListBox lstControllers;
        private ListView lstSwipe;
        private string[] RecordDetails = new string[] { 
            "1", "SwipePass", "Swipe", "刷卡开门", "2", "SwipePass", "Swipe Close", "刷卡关", "3", "SwipePass", "Swipe Open", "刷卡开", "4", "SwipePass", "Swipe Limited Times", "刷卡开门(带限次)",
            "5", "SwipeNOPass", "Denied Access: PC Control", "刷卡禁止通过: 电脑控制", "6", "SwipeNOPass", "Denied Access: No PRIVILEGE", "刷卡禁止通过: 没有权限", "7", "SwipeNOPass", "Denied Access: Wrong PASSWORD", "刷卡禁止通过: 密码不对", "8", "SwipeNOPass", "Denied Access: AntiBack", "刷卡禁止通过: 反潜回",
            "9", "SwipeNOPass", "Denied Access: More Cards", "刷卡禁止通过: 多卡", "10", "SwipeNOPass", "Denied Access: First Card Open", "刷卡禁止通过: 首卡", "11", "SwipeNOPass", "Denied Access: Door Set NC", "刷卡禁止通过: 门为常闭", "12", "SwipeNOPass", "Denied Access: InterLock", "刷卡禁止通过: 互锁",
            "13", "SwipeNOPass", "Denied Access: Limited Times", "刷卡禁止通过: 受刷卡次数限制", "14", "SwipeNOPass", "Denied Access: Limited Person Indoor", "刷卡禁止通过: 门内人数限制", "15", "SwipeNOPass", "Denied Access: Invalid Timezone", "刷卡禁止通过: 卡过期或不在有效时段", "16", "SwipeNOPass", "Denied Access: In Order", "刷卡禁止通过: 按顺序进出限制",
            "17", "SwipeNOPass", "Denied Access: SWIPE GAP LIMIT", "刷卡禁止通过: 刷卡间隔约束", "18", "SwipeNOPass", "Denied Access", "刷卡禁止通过: 原因不明", "19", "SwipeNOPass", "Denied Access: Limited Times", "刷卡禁止通过: 刷卡次数限制", "20", "ValidEvent", "Push Button", "按钮开门",
            "21", "ValidEvent", "Push Button Open", "按钮开", "22", "ValidEvent", "Push Button Close", "按钮关", "23", "ValidEvent", "Door Open", "门打开[门磁信号]", "24", "ValidEvent", "Door Closed", "门关闭[门磁信号]",
            "25", "ValidEvent", "Super Password Open Door", "超级密码开门", "26", "ValidEvent", "Super Password Open", "超级密码开", "27", "ValidEvent", "Super Password Close", "超级密码关", "28", "Warn", "Controller Power On", "控制器上电",
            "29", "Warn", "Controller Reset", "控制器复位", "30", "Warn", "Push Button Invalid: Disable", "按钮不开门: 按钮禁用", "31", "Warn", "Push Button Invalid: Forced Lock", "按钮不开门: 强制关门", "32", "Warn", "Push Button Invalid: Not On Line", "按钮不开门: 门不在线",
            "33", "Warn", "Push Button Invalid: InterLock", "按钮不开门: 互锁", "34", "Warn", "Threat", "胁迫报警", "35", "Warn", "Threat Open", "胁迫报警开", "36", "Warn", "Threat Close", "胁迫报警关",
            "37", "Warn", "Open too long", "门长时间未关报警[合法开门后]", "38", "Warn", "Forced Open", "强行闯入报警", "39", "Warn", "Fire", "火警", "40", "Warn", "Forced Close", "强制关门",
            "41", "Warn", "Guard Against Theft", "防盗报警", "42", "Warn", "7*24Hour Zone", "烟雾煤气温度报警", "43", "Warn", "Emergency Call", "紧急呼救报警", "44", "RemoteOpen", "Remote Open Door", "操作员远程开门",
            "45", "RemoteOpen", "Remote Open Door By USB Reader", "发卡器确定发出的远程开门"
        };
        private Thread tRunBatch;
        private TextBox txt64Command;
        private TextBox txtCardNO;
        private TextBox txtInfo;
        private TextBox txtSN;
        private TextBox txtWatchServerIP;
        private TextBox txtWatchServerPort;
        private wgUdpServerCom udpserverLast;

        public FormCloudServer()
        {
            this.InitializeComponent();
        }

        private void addSwipeItems(ListViewItem info)
        {
            if (this.lstSwipe.Items.Count > 0x7d00)
            {
                for (int i = 0; i < 0x3e80; i++)
                {
                    this.lstSwipe.Items.RemoveAt(0x3e7f - i);
                }
            }
            this.lstSwipe.Items.Add(info);
        }

        private int adjustTime(ref wgUdpServerCom serverCurrent, long sn)
        {
            int num = -13;
            byte[] destinationArray = new byte[0x40];
            for (int i = 0; i < 0x40; i++)
            {
                destinationArray[i] = 0;
            }
            destinationArray[0] = 0x17;
            destinationArray[1] = 0x30;
            Array.Copy(BitConverter.GetBytes(sn), 0, destinationArray, 4, 4);
            DateTime now = DateTime.Now;
            destinationArray[8] = (byte) this.GetHex((now.Year - (now.Year % 100)) / 100);
            destinationArray[9] = (byte) this.GetHex(now.Year % 100);
            destinationArray[10] = (byte) this.GetHex(now.Month);
            destinationArray[11] = (byte) this.GetHex(now.Day);
            destinationArray[12] = (byte) this.GetHex(now.Hour);
            destinationArray[13] = (byte) this.GetHex(now.Minute);
            destinationArray[14] = (byte) this.GetHex(now.Second);
            int num3 = 3;
            long sequenceId = 0L;
            while (num3 > 0)
            {
                serverCurrent.receivedReplyClear();
                sequenceId = serverCurrent.UDP_OnlySend(destinationArray, sequenceId);
                if (sequenceId >= 0L)
                {
                    this.log($" ====> 发出校准时间 指令 控制器SN={sn.ToString()},  流水号= 0x{sequenceId:X} ");
                }
                else
                {
                    this.log($"====>??? 失败: 发出校准时间指令 控制器SN={sn.ToString()} ");
                    this.log("控制器 没有连接...");
                    return num;
                }
                int num5 = 100;
                while (num5-- > 0)
                {
                    if (this.udpserverLast.receivedReplyCount() > 0)
                    {
                        byte[] buff = this.udpserverLast.getReply();
                        if (this.byteToLong(buff, 40, 4) != sequenceId)
                        {
                            continue;
                        }
                        this.log($"====>收到返回 信息 {BitConverter.ToString(buff)} ");
                        bool flag = true;
                        for (int j = 0; j < 8; j++)
                        {
                            if (destinationArray[8 + j] != buff[8 + j])
                            {
                                flag = false;
                                break;
                            }
                        }
                        this.log(flag ? "成功!\r\n" : "失败...\r\n");
                        if (flag)
                        {
                            num = 1;
                        }
                        break;
                    }
                    Thread.Sleep(10);
                }
                if ((num5 >= 0) || (num >= 0))
                {
                    break;
                }
                num3--;
            }
            if (num <= 0)
            {
                this.log("失败...\r\n");
            }
            return num;
        }

        private void btnAdjustTime_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtSN.Text) && (this.lstControllers.Items.Count > 0))
            {
                this.txtSN.Text = this.lstControllers.Items[0].ToString();
            }
            if (!string.IsNullOrEmpty(this.txtSN.Text))
            {
                long sn = long.Parse(this.txtSN.Text);
                this.adjustTime(ref this.udpserverLast, sn);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtInfo.Text = "";
        }

        private void btnControllerInfo_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtSN.Text) && (this.lstControllers.Items.Count > 0))
            {
                this.txtSN.Text = this.lstControllers.Items[0].ToString();
            }
            if (!string.IsNullOrEmpty(this.txtSN.Text))
            {
                long sn = long.Parse(this.txtSN.Text);
                wgControllerInfo controllerInfo = this.udpserverLast.GetControllerInfo((int) sn);
                if (controllerInfo != null)
                {
                    string info = "";
                    info = ((info + $"控制器序列号SN = {controllerInfo.ControllerSN}") + $"通信IP = {controllerInfo.IP}" + $"通信PORT = {controllerInfo.PORT}") + $"刷新时间 = {controllerInfo.UpdateDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff")}" + $"接收数据 = {controllerInfo.ReceivedBytes}";
                    this.log(info);
                    string[] strArray = controllerInfo.ReceivedBytes.Split(new char[] { '-' });
                    byte[] recv = new byte[strArray.Length];
                    for (int i = 0; i < recv.Length; i++)
                    {
                        recv[i] = (byte) int.Parse(strArray[i], NumberStyles.AllowHexSpecifier);
                    }
                    if ((recv.Length == 0x40) && (recv[1] == 0x20))
                    {
                        this.log(" 查询控制器状态...");
                        this.displayRecordInformation(recv);
                        int[] numArray = new int[] { recv[0x1c], recv[0x1d], recv[30], recv[0x1f] };
                        int[] numArray2 = new int[] { recv[0x20], recv[0x21], recv[0x22], recv[0x23] };
                        byte num1 = recv[0x24];
                        this.byteToLong(recv, 0x2c, 4);
                        int num3 = recv[0x31];
                        int num15 = num3 & 1;
                        int num16 = num3 & 2;
                        int num17 = num3 & 4;
                        int num18 = num3 & 8;
                        int num4 = recv[50];
                        int num19 = num4 & 1;
                        int num20 = num4 & 2;
                        if ((num4 & 8) > 0)
                        {
                            this.log($"扩展板输入口/输出口数据 驱动要求是V7.72 [正常连接时起作用] 低4bits为输出口 ={recv[0x36] & 15}, 高4bits为输入口 ={(recv[0x36] >> 4) & 15} ");
                        }
                        string str2 = "2000-01-01 00:00:00";
                        str2 = $"{0x20:X2}{recv[0x33]:X2}-{recv[0x34]:X2}-{recv[0x35]:X2} {recv[0x25]:X2}:{recv[0x26]:X2}:{recv[0x27]:X2}";
                        this.log($"控制器时间:{str2}");
                        if (wgControllerInfo.IsElevator((int) sn))
                        {
                            this.log($"一对多信息[输入]: {recv[0x20]:X2}-{recv[0x21]:X2}-{recv[0x22]:X2} {recv[0x1c]:X2}-{recv[0x1d]:X2}-{recv[30]:X2}");
                            string str3 = "";
                            string str4 = "";
                            int num5 = 0;
                            int num6 = 0;
                            if ((recv[0x22] & 0x80) > 0)
                            {
                                this.log(string.Format("一对多信息 已连接1-20输入口 ", new object[0]));
                                long num7 = (recv[0x20] + (recv[0x21] << 8)) + (recv[0x22] << 0x10);
                                for (int j = 0; j < 20; j++)
                                {
                                    if ((num7 & (((int) 1) << j)) > 0L)
                                    {
                                        str4 = str4 + ((j + 1)).ToString() + ",";
                                        num6++;
                                    }
                                    else
                                    {
                                        str3 = str3 + ((j + 1)).ToString() + ",";
                                        num5++;
                                    }
                                }
                            }
                            if ((recv[30] & 0x80) > 0)
                            {
                                this.log(string.Format("一对多信息 已连接21-40输入口 ", new object[0]));
                                long num9 = (recv[0x1c] + (recv[0x1d] << 8)) + (recv[30] << 0x10);
                                for (int k = 0; k < 20; k++)
                                {
                                    if ((num9 & (((int) 1) << k)) > 0L)
                                    {
                                        str4 = str4 + ((k + 0x15)).ToString() + ",";
                                        num6++;
                                    }
                                    else
                                    {
                                        str3 = str3 + ((k + 0x15)).ToString() + ",";
                                        num5++;
                                    }
                                }
                            }
                            if (!string.IsNullOrEmpty(str3) || !string.IsNullOrEmpty(str4))
                            {
                                this.log($"多输入口,已短接的端口有{num5}个,分别是: {str3} ");
                                this.log($"多输入口,空端口有{num6}个,分别是: {str4} ");
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(this.txtSN.Text))
                    {
                        this.getControllerInfo(ref this.udpserverLast, sn);
                    }
                }
            }
        }

        private void btnGetAllSwipes_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtSN.Text) && (this.lstControllers.Items.Count > 0))
            {
                this.txtSN.Text = this.lstControllers.Items[0].ToString();
            }
            if (!string.IsNullOrEmpty(this.txtSN.Text))
            {
                if ((this.tRunBatch != null) && this.tRunBatch.IsAlive)
                {
                    MessageBox.Show("正在提取记录");
                }
                else
                {
                    long num = long.Parse(this.txtSN.Text);
                    ArrayList list = new ArrayList();
                    if (this.chkAllControllers.Checked)
                    {
                        for (int i = 0; i < this.lstControllers.Items.Count; i++)
                        {
                            list.Add(long.Parse(this.lstControllers.Items[i].ToString()));
                        }
                    }
                    else
                    {
                        list.Add(num);
                    }
                    this.tRunBatch = new Thread(new ParameterizedThreadStart(this.getSwipeRecords));
                    this.tRunBatch.Name = "Batch Running ";
                    this.tRunBatch.Start(new object[] { this.udpserverLast, list });
                }
            }
        }

        private void btnOpenFloor_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtSN.Text) && (this.lstControllers.Items.Count > 0))
            {
                this.txtSN.Text = this.lstControllers.Items[0].ToString();
            }
            if (!string.IsNullOrEmpty(this.txtSN.Text))
            {
                long sn = long.Parse(this.txtSN.Text);
                if (!wgControllerInfo.IsElevator((int) sn))
                {
                    MessageBox.Show("此控制器不是一对多(梯控)设备");
                }
                else
                {
                    int floorNO = 1;
                    if (this.cboFloors.SelectedIndex >= 0)
                    {
                        floorNO = this.cboFloors.SelectedIndex + 1;
                    }
                    long cardNO = 0x1869fL;
                    this.remoteOpenFloor(ref this.udpserverLast, sn, floorNO, cardNO);
                }
            }
        }

        private void btnRemoteOpenDoor_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtSN.Text) && (this.lstControllers.Items.Count > 0))
            {
                this.txtSN.Text = this.lstControllers.Items[0].ToString();
            }
            if (!string.IsNullOrEmpty(this.txtSN.Text))
            {
                long sn = long.Parse(this.txtSN.Text);
                int doorNO = 1;
                if (this.cboDoors.SelectedIndex >= 0)
                {
                    doorNO = this.cboDoors.SelectedIndex + 1;
                    if (wgControllerInfo.GetControllerType((int) sn) < doorNO)
                    {
                        MessageBox.Show("此控制器不存在 选定的门号...");
                        return;
                    }
                }
                int inOrOut = 1;
                long cardNO = 0x1869fL;
                this.remoteOpenDoor(ref this.udpserverLast, sn, doorNO, inOrOut, cardNO);
            }
        }

        private void btnSendCommand_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtSN.Text) && (this.lstControllers.Items.Count > 0))
            {
                this.txtSN.Text = this.lstControllers.Items[0].ToString();
            }
            if (!string.IsNullOrEmpty(this.txtSN.Text))
            {
                long num = long.Parse(this.txtSN.Text);
                string text = this.txt64Command.Text;
                byte[] bytArr = new byte[0x40];
                if (text.Split(new char[] { ' ' }).Length == 0x3f)
                {
                    string[] strArray = text.Split(new char[] { ' ' });
                    for (int i = 0; i < 0x40; i++)
                    {
                        bytArr[i] = byte.Parse(strArray[i], NumberStyles.AllowHexSpecifier);
                    }
                }
                else if (text.Replace(" ", "").Length == 0x80)
                {
                    strToByteArr(text.Replace(" ", ""), ref bytArr, bytArr.Length);
                }
                else
                {
                    MessageBox.Show("输入的指令无效");
                    return;
                }
                Array.Copy(BitConverter.GetBytes(num), 0, bytArr, 4, 4);
                this.sendCust64Byte(ref this.udpserverLast, num, bytArr);
            }
        }

        private void btnUploadAllPrivileges_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtSN.Text) && (this.lstControllers.Items.Count > 0))
            {
                this.txtSN.Text = this.lstControllers.Items[0].ToString();
            }
            if (!string.IsNullOrEmpty(this.txtSN.Text))
            {
                if ((this.tRunBatch != null) && this.tRunBatch.IsAlive)
                {
                    MessageBox.Show("正在上传权限或提取记录...");
                }
                else
                {
                    long num = long.Parse(this.txtSN.Text);
                    ArrayList list = new ArrayList();
                    if (this.chkAllControllerB.Checked)
                    {
                        for (int i = 0; i < this.lstControllers.Items.Count; i++)
                        {
                            list.Add(long.Parse(this.lstControllers.Items[i].ToString()));
                        }
                    }
                    else
                    {
                        list.Add(num);
                    }
                    this.tRunBatch = new Thread(new ParameterizedThreadStart(this.uploadAllPrivlieges));
                    this.tRunBatch.Name = "Batch Running ";
                    this.tRunBatch.Start(new object[] { this.udpserverLast, list });
                }
            }
        }

        private void btnUploadPrivilegeSingle_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtSN.Text) && (this.lstControllers.Items.Count > 0))
            {
                this.txtSN.Text = this.lstControllers.Items[0].ToString();
            }
            if (!string.IsNullOrEmpty(this.txtSN.Text))
            {
                long sn = long.Parse(this.txtSN.Text);
                long cardNO = long.Parse(this.txtCardNO.Text);
                this.uploadPrivilegSingle(ref this.udpserverLast, sn, cardNO, this.dtpActivate.Value, this.dtpDeactivate.Value);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.bStopWatchServer = true;
            this.groupBox1.Enabled = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (this.bStopWatchServer)
            {
                this.bStopWatchServer = false;
                this.button4.BackColor = Color.Yellow;
                this.groupBox1.Enabled = true;
                this.WatchingServerRuning(this.txtWatchServerIP.Text, int.Parse(this.txtWatchServerPort.Text));
                this.button4.BackColor = Color.Transparent;
                this.bStopWatchServer = true;
            }
        }

        private long byteToLong(byte[] buff, int start, int len)
        {
            long num = 0L;
            for (int i = 0; (i < len) && (i < 4); i++)
            {
                long num3 = buff[i + start];
                num += num3 << (8 * i);
            }
            return num;
        }

        private void chkDisplayIP_CheckedChanged(object sender, EventArgs e)
        {
        }

        private void chkLogDetail_CheckedChanged(object sender, EventArgs e)
        {
            this.bDisplayDetail = this.chkLogDetail.Checked;
        }

        private void dealQRData(byte[] recv, ref wgUdpServerCom server)
        {
            int length = 0;
            length = (int) this.byteToLong(recv, 8, 4);
            byte num1 = recv[13];
            int doorNO = recv[14];
            int inOrOut = recv[15];
            long num4 = this.byteToLong(recv, 40, 4);
            if (length >= 1)
            {
                byte[] destinationArray = new byte[length];
                Array.Copy(recv, 0x40, destinationArray, 0, length);
                this.log($"流水号={num4} 二维码原始数据:{BitConverter.ToString(destinationArray)}".Replace('-', ' '));
                this.log($"流水号={num4} 二维码原始数据(转换为字符串):{Encoding.GetEncoding("GB2312").GetString(destinationArray).Trim()}");
                if (this.checkBox2.Checked)
                {
                    long cardNO = 0L;
                    cardNO = num4;
                    long sn = this.byteToLong(recv, 4, 4);
                    this.remoteOpenDoor(ref server, sn, doorNO, inOrOut, cardNO);
                }
            }
        }

        private void dealSwipeRecord(byte[] recv, ref wgUdpServerCom server)
        {
            this.byteToLong(recv, 8, 4);
            int num = recv[12];
            int num2 = recv[13];
            int doorNO = recv[14];
            int inOrOut = recv[15];
            long cardNO = 0L;
            cardNO = this.byteToLong(recv, 0x10, 4);
            long num6 = 0L;
            num6 = this.byteToLong(recv, 0x2c, 4);
            cardNO += num6 << 0x20;
            string str = "2000-01-01 00:00:00";
            str = $"{recv[20]:X2}{recv[0x15]:X2}-{recv[0x16]:X2}-{recv[0x17]:X2} {recv[0x18]:X2}:{recv[0x19]:X2}:{recv[0x1a]:X2}";
            byte num1 = recv[0x1b];
            if (((num == 1) && (num2 == 0)) && this.checkBox1.Checked)
            {
                long sn = this.byteToLong(recv, 4, 4);
                this.remoteOpenDoor(ref server, sn, doorNO, inOrOut, cardNO);
            }
        }

        private void displayRecordInformation(byte[] recv)
        {
            int num = 0;
            num = (int) this.byteToLong(recv, 8, 4);
            int num2 = recv[12];
            int num3 = recv[13];
            int num4 = recv[14];
            int num5 = recv[15];
            long num6 = 0L;
            num6 = this.byteToLong(recv, 0x10, 4);
            long num7 = 0L;
            num7 = this.byteToLong(recv, 0x2c, 4);
            num6 += num7 << 0x20;
            string str = "2000-01-01 00:00:00";
            str = $"{recv[20]:X2}{recv[0x15]:X2}-{recv[0x16]:X2}-{recv[0x17]:X2} {recv[0x18]:X2}:{recv[0x19]:X2}:{recv[0x1a]:X2}";
            int reason = recv[0x1b];
            switch (num2)
            {
                case 0:
                    this.log($"索引位={num}  无记录");
                    return;

                case 0xff:
                    this.log(" 指定索引位的记录已被覆盖掉了,请使用索引0, 取回最早一条记录的索引值");
                    return;

                case 1:
                {
                    this.log($"索引位={num}  ");
                    this.log($"  卡号 = {num6}");
                    this.log($"  门号 = {num4}");
                    //this.log($"  进出 = {(num5 == 1) ? "进门" : "出门"}");
                    //this.log($"  有效 = {(num3 == 1) ? "通过" : "禁止"}");
                    this.log($"  时间 = {str}");
                    this.log($"  描述 = {this.getReasonDetailChinese(reason)}");
                    long num9 = this.byteToLong(recv, 4, 4);
                    ListViewItem item = new ListViewItem(new string[] { num6.ToString(), str, num9.ToString(), num4.ToString() });
                    base.Invoke(new dlgAddSwipeItems(this.addSwipeItems), new object[] { item });
                    return;
                }
                case 2:
                    this.log($"索引位={num}  非刷卡记录");
                    if ((reason == 0x2c) && (num6 > 1L))
                    {
                        this.log($"  模拟卡号 = {num6}");
                    }
                    else
                    {
                        this.log($"  编号 = {num6}");
                    }
                    this.log($"  门号 = {num4}");
                    this.log($"  时间 = {str}");
                    this.log($"  描述 = {this.getReasonDetailChinese(reason)}");
                    return;

                case 3:
                    this.log($"索引位={num}  报警记录");
                    this.log($"  编号 = {num6}");
                    this.log($"  门号 = {num4}");
                    this.log($"  时间 = {str}");
                    this.log($"  描述 = {this.getReasonDetailChinese(reason)}");
                    break;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FormCloudServer_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.bStopWatchServer = true;
        }

        private void FormCloudServer_Load(object sender, EventArgs e)
        {
            this.cboFloors.Items.Clear();
            for (int i = 0; i < 40; i++)
            {
                this.cboFloors.Items.Add($"{i + 1}楼");
            }
            this.cboFloors.SelectedIndex = 0;
            string hostName = Dns.GetHostName();
            bool flag = false;
            foreach (IPAddress address in Dns.GetHostEntry(hostName).AddressList)
            {
                if (((address.AddressFamily == AddressFamily.InterNetwork) && !address.IsIPv6LinkLocal) && (address.ToString() != "127.0.0.1"))
                {
                    if (flag)
                    {
                        MessageBox.Show("电脑存在多个IP, 建议前期开发时只使用一个IP操作.  [假如无线与网线口同时在用时, 请关键无线口]");
                        break;
                    }
                    flag = true;
                    this.txtWatchServerIP.Text = address.ToString();
                }
            }
            if (!flag)
            {
                MessageBox.Show("网络不通! 请接好网线..");
            }
        }

        private int getControllerInfo(ref wgUdpServerCom serverCurrent, long sn)
        {
            int num = -13;
            byte[] destinationArray = new byte[0x40];
            for (int i = 0; i < 0x40; i++)
            {
                destinationArray[i] = 0;
            }
            destinationArray[0] = 0x17;
            destinationArray[1] = 0x94;
            Array.Copy(BitConverter.GetBytes(sn), 0, destinationArray, 4, 4);
            int num3 = 3;
            long sequenceId = 0L;
            while (num3 > 0)
            {
                serverCurrent.receivedReplyClear();
                sequenceId = serverCurrent.UDP_OnlySend(destinationArray, sequenceId);
                if (sequenceId < 0L)
                {
                    this.log($"====>??? 失败: 发出指令 控制器SN={sn.ToString()} ");
                    this.log("控制器 没有连接...");
                    return num;
                }
                int num5 = 100;
                while (num5-- > 0)
                {
                    if (this.udpserverLast.receivedReplyCount() > 0)
                    {
                        byte[] buff = this.udpserverLast.getReply();
                        if (this.byteToLong(buff, 40, 4) != sequenceId)
                        {
                            continue;
                        }
                        int index = 8;
                        this.log($"控制器配置IP: {buff[index]}.{buff[index + 1]}.{buff[index + 2]}.{buff[index + 3]}");
                        index = 12;
                        this.log($"控制器配置掩码: {buff[index]}.{buff[index + 1]}.{buff[index + 2]}.{buff[index + 3]}");
                        index = 0x10;
                        this.log($"控制器配置网关: {buff[index]}.{buff[index + 1]}.{buff[index + 2]}.{buff[index + 3]}");
                        index = 20;
                        this.log($"控制器配置MAC: {buff[index]:X2}:{buff[index + 1]:X2}:{buff[index + 2]:X2}:{buff[index + 3]:X2}:{buff[index + 4]:X2}:{buff[index + 5]:X2}");
                        index = 0x1a;
                        this.log($"驱动版本: V{buff[index]:X}.{buff[index + 1]:X}");
                        num = 1;
                        break;
                    }
                    Thread.Sleep(10);
                }
                if ((num5 >= 0) || (num >= 0))
                {
                    break;
                }
                num3--;
            }
            if (num <= 0)
            {
                this.log("失败...\r\n");
            }
            return num;
        }

        private int GetHex(int val) => 
            ((val % 10) + ((((val - (val % 10)) / 10) % 10) * 0x10));

        private string getReasonDetailChinese(int Reason)
        {
            if (Reason > 0x2d)
            {
                return "";
            }
            if (Reason <= 0)
            {
                return "";
            }
            return this.RecordDetails[((Reason - 1) * 4) + 3];
        }

        private string getReasonDetailEnglish(int Reason)
        {
            if (Reason > 0x2d)
            {
                return "";
            }
            if (Reason <= 0)
            {
                return "";
            }
            return this.RecordDetails[((Reason - 1) * 4) + 2];
        }

        private void getSwipeRecords(object arg)
        {
            object[] objArray = arg as object[];
            wgUdpServerCom serverCurrent = (wgUdpServerCom) objArray[0];
            ArrayList list = (ArrayList) objArray[1];
            for (int i = 0; i < list.Count; i++)
            {
                int num18;
                long sn = (long) list[i];
                int num3 = 0;
                int num4 = 0;
                byte[] destinationArray = new byte[0x400];
                long num5 = 0L;
                long num6 = 0L;
                long num7 = 0L;
                long val = 0L;
                this.log("控制器SN = " + sn.ToString());
                this.log("1.9 提取记录操作\t 开始...[1024字节指令]");
                byte[] outBytes = new byte[0x40];
                for (int j = 0; j < 0x40; j++)
                {
                    outBytes[j] = 0;
                }
                outBytes[0] = 0x17;
                outBytes[1] = 0xb0;
                val = 0L;
                this.LongToBytes(ref outBytes, 8, val);
                byte[] recv = null;
                num3 = this.runComm(ref serverCurrent, sn, outBytes, ref recv);
                num4 = 0;
                if (num3 >= 0)
                {
                    num5 = (int) this.byteToLong(recv, 8, 4);
                    this.log(" 获取最早一条记录索引\t =" + num5.ToString());
                }
                if (num3 >= 0)
                {
                    for (int num10 = 0; num10 < 0x40; num10++)
                    {
                        outBytes[num10] = 0;
                    }
                    outBytes[0] = 0x17;
                    outBytes[1] = 0xb0;
                    val = 0xffffffffL;
                    this.LongToBytes(ref outBytes, 8, val);
                    recv = null;
                    num3 = this.runComm(ref serverCurrent, sn, outBytes, ref recv);
                }
                if (num3 >= 0)
                {
                    num6 = (int) this.byteToLong(recv, 8, 4);
                    this.log(" 获取最后一条记录索引\t  =" + num6.ToString());
                }
                if (this.chkGetAllSwipes.Checked)
                {
                    num7 = num5 - 1L;
                }
                else
                {
                    if (num3 >= 0)
                    {
                        for (int num11 = 0; num11 < 0x40; num11++)
                        {
                            outBytes[num11] = 0;
                        }
                        outBytes[0] = 0x17;
                        outBytes[1] = 180;
                        val = 0L;
                        this.LongToBytes(ref outBytes, 8, val);
                        recv = null;
                        num3 = this.runComm(ref serverCurrent, sn, outBytes, ref recv);
                    }
                    if (num3 >= 0)
                    {
                        num7 = (int) this.byteToLong(recv, 8, 4);
                        this.log("获取已读取过的记录索引号\t  =" + num7.ToString());
                    }
                }
                long num12 = 0L;
                if (num3 < 0)
                {
                    continue;
                }
                long num13 = 0L;
                long num14 = num7 + 1L;
                if ((num7 > num6) || (num7 < num5))
                {
                    num14 = num5;
                }
                int num16 = 0;
                for (int k = 0; k < 0x40; k++)
                {
                    outBytes[k] = 0;
                }
                outBytes[0] = 0x17;
                outBytes[1] = 0xb0;
                val = 0L;
                this.LongToBytes(ref outBytes, 8, val);
                Array.Copy(BitConverter.GetBytes(sn), 0, outBytes, 4, 4);
                recv = null;
            Label_0267:
                num18 = 0;
                while (num18 < 0x400)
                {
                    destinationArray[num18] = 0;
                    num18++;
                }
                long num15 = num14;
                for (int m = 0; m < 0x400; m += 0x40)
                {
                    this.LongToBytes(ref outBytes, 8, num14);
                    Array.Copy(outBytes, 0, destinationArray, m, 0x40);
                    num14 += 1L;
                    num16++;
                }
                num3 = this.runComm(ref serverCurrent, sn, destinationArray, ref recv);
                num4 = 0;
                if (num3 < 0)
                {
                    goto Label_0377;
                }
                for (int n = 0; n < 0x400; n += 0x40)
                {
                    num4 = 0;
                    byte[] buffer4 = new byte[0x40];
                    Array.Copy(recv, n, buffer4, 0, 0x40);
                    switch (buffer4[12])
                    {
                        case 0:
                            num4 = 2;
                            goto Label_0366;

                        case 0xff:
                            num4 = 0;
                            goto Label_0366;
                    }
                    num4 = 1;
                    num13 = num15;
                    num15 += 1L;
                    num12 += 1L;
                    if (num12 < 100L)
                    {
                        this.displayRecordInformation(buffer4);
                        if (num12 == 0x63L)
                        {
                            this.log(" 为加快提取速度, 超过100个的  不再显示记录信息.......");
                            Application.DoEvents();
                        }
                    }
                }
            Label_0366:
                if ((num4 == 1) && (num16 < 0x30d40))
                {
                    goto Label_0267;
                }
            Label_0377:
                this.log("1.9 完全提取成功\t ... 有效记录数= " + num12.ToString());
                if ((num4 > 0) && (num12 > 0L))
                {
                    for (int num22 = 0; num22 < 0x40; num22++)
                    {
                        outBytes[num22] = 0;
                    }
                    outBytes[0] = 0x17;
                    outBytes[1] = 0xb2;
                    val = 0L;
                    this.LongToBytes(ref outBytes, 8, num13);
                    this.LongToBytes(ref outBytes, 12, 0x55aaaa55L);
                    Array.Copy(BitConverter.GetBytes(sn), 0, outBytes, 4, 4);
                    recv = null;
                    num3 = this.runComm(ref serverCurrent, sn, outBytes, ref recv);
                    num4 = 0;
                    if ((num3 >= 0) && (recv[8] == 1))
                    {
                        this.log("1.9 完全提取成功\t 成功...");
                        num4 = 1;
                    }
                }
            }
        }

        private void InitializeComponent()
        {
            this.button4 = new Button();
            this.button2 = new Button();
            this.txtWatchServerPort = new TextBox();
            this.label3 = new Label();
            this.txtWatchServerIP = new TextBox();
            this.label5 = new Label();
            this.txtInfo = new TextBox();
            this.groupBox1 = new GroupBox();
            this.btnSendCommand = new Button();
            this.txt64Command = new TextBox();
            this.label10 = new Label();
            this.chkLogDetail = new CheckBox();
            this.groupBox4 = new GroupBox();
            this.cboFloors = new ComboBox();
            this.btnOpenFloor = new Button();
            this.cboDoors = new ComboBox();
            this.btnControllerInfo = new Button();
            this.groupBox2 = new GroupBox();
            this.chkAllControllerB = new CheckBox();
            this.btnUploadAllPrivileges = new Button();
            this.btnUploadPrivilegeSingle = new Button();
            this.txtCardNO = new TextBox();
            this.label9 = new Label();
            this.dateEndHMS1 = new DateTimePicker();
            this.dtpDeactivate = new DateTimePicker();
            this.dtpActivate = new DateTimePicker();
            this.label7 = new Label();
            this.label8 = new Label();
            this.btnAdjustTime = new Button();
            this.chkAllControllers = new CheckBox();
            this.chkGetAllSwipes = new CheckBox();
            this.btnGetAllSwipes = new Button();
            this.btnRemoteOpenDoor1 = new Button();
            this.checkBox2 = new CheckBox();
            this.checkBox1 = new CheckBox();
            this.txtSN = new TextBox();
            this.label1 = new Label();
            this.lstControllers = new ListBox();
            this.lstSwipe = new ListView();
            this.columnHeader1 = new ColumnHeader();
            this.columnHeader2 = new ColumnHeader();
            this.columnHeader3 = new ColumnHeader();
            this.columnHeader4 = new ColumnHeader();
            this.label2 = new Label();
            this.label4 = new Label();
            this.label6 = new Label();
            this.groupBox3 = new GroupBox();
            this.btnClear = new Button();
            this.chkDisplayIP = new CheckBox();
            this.groupBox1.SuspendLayout();
            this.groupBox4.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            base.SuspendLayout();
            this.button4.Location = new Point(0x39, 0x4e);
            this.button4.Name = "button4";
            this.button4.Size = new Size(0xd8, 0x17);
            this.button4.TabIndex = 10;
            this.button4.Text = "打开接收服务器";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new EventHandler(this.button4_Click);
            this.button2.Location = new Point(0x1d, 20);
            this.button2.Name = "button2";
            this.button2.Size = new Size(0xd8, 0x17);
            this.button2.TabIndex = 12;
            this.button2.Text = "Stop 停止监控或测试";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new EventHandler(this.button2_Click);
            this.txtWatchServerPort.Location = new Point(0xad, 0x33);
            this.txtWatchServerPort.Name = "txtWatchServerPort";
            this.txtWatchServerPort.Size = new Size(50, 0x15);
            this.txtWatchServerPort.TabIndex = 11;
            this.txtWatchServerPort.Text = "61005";
            this.label3.AutoSize = true;
            this.label3.Location = new Point(60, 0x30);
            this.label3.Name = "label3";
            this.label3.Size = new Size(0x6b, 0x18);
            this.label3.TabIndex = 13;
            this.label3.Text = "Watch Server Port\r\n接收服务器端口号";
            this.txtWatchServerIP.Location = new Point(0xad, 12);
            this.txtWatchServerIP.Name = "txtWatchServerIP";
            this.txtWatchServerIP.Size = new Size(100, 0x15);
            this.txtWatchServerIP.TabIndex = 8;
            this.txtWatchServerIP.Text = "192.168.168.101";
            this.label5.AutoSize = true;
            this.label5.Location = new Point(60, 15);
            this.label5.Name = "label5";
            this.label5.Size = new Size(0x65, 0x18);
            this.label5.TabIndex = 9;
            this.label5.Text = "Watch Server IP\r\n接收服务器IP地址";
            this.txtInfo.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.txtInfo.Location = new Point(5, 0x21);
            this.txtInfo.Multiline = true;
            this.txtInfo.Name = "txtInfo";
            this.txtInfo.ScrollBars = ScrollBars.Both;
            this.txtInfo.Size = new Size(0x1ef, 0x182);
            this.txtInfo.TabIndex = 14;
            this.groupBox1.Controls.Add(this.btnSendCommand);
            this.groupBox1.Controls.Add(this.txt64Command);
            this.groupBox1.Controls.Add(this.label10);
            this.groupBox1.Controls.Add(this.chkLogDetail);
            this.groupBox1.Controls.Add(this.groupBox4);
            this.groupBox1.Controls.Add(this.cboDoors);
            this.groupBox1.Controls.Add(this.btnControllerInfo);
            this.groupBox1.Controls.Add(this.groupBox2);
            this.groupBox1.Controls.Add(this.btnAdjustTime);
            this.groupBox1.Controls.Add(this.chkAllControllers);
            this.groupBox1.Controls.Add(this.chkGetAllSwipes);
            this.groupBox1.Controls.Add(this.btnGetAllSwipes);
            this.groupBox1.Controls.Add(this.btnRemoteOpenDoor1);
            this.groupBox1.Controls.Add(this.checkBox2);
            this.groupBox1.Controls.Add(this.checkBox1);
            this.groupBox1.Controls.Add(this.txtSN);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.button2);
            this.groupBox1.Enabled = false;
            this.groupBox1.Location = new Point(0x1c, 0x62);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new Size(0x408, 180);
            this.groupBox1.TabIndex = 15;
            this.groupBox1.TabStop = false;
            this.btnSendCommand.ImeMode = ImeMode.NoControl;
            this.btnSendCommand.Location = new Point(0x386, 0x8e);
            this.btnSendCommand.Name = "btnSendCommand";
            this.btnSendCommand.Size = new Size(70, 0x17);
            this.btnSendCommand.TabIndex = 0x1d;
            this.btnSendCommand.Text = "发送指令";
            this.btnSendCommand.UseVisualStyleBackColor = true;
            this.btnSendCommand.Click += new EventHandler(this.btnSendCommand_Click);
            this.txt64Command.Location = new Point(0x2a6, 0x8e);
            this.txt64Command.MaxLength = 0xc0;
            this.txt64Command.Multiline = true;
            this.txt64Command.Name = "txt64Command";
            this.txt64Command.Size = new Size(0xda, 0x20);
            this.txt64Command.TabIndex = 0x1b;
            this.label10.AutoSize = true;
            this.label10.ImeMode = ImeMode.NoControl;
            this.label10.Location = new Point(0x25f, 0x91);
            this.label10.Name = "label10";
            this.label10.Size = new Size(0x41, 12);
            this.label10.TabIndex = 0x1c;
            this.label10.Text = "64字节指令";
            this.chkLogDetail.AutoSize = true;
            this.chkLogDetail.Checked = true;
            this.chkLogDetail.CheckState = CheckState.Checked;
            this.chkLogDetail.Location = new Point(0x22, 0x70);
            this.chkLogDetail.Name = "chkLogDetail";
            this.chkLogDetail.Size = new Size(0x60, 0x10);
            this.chkLogDetail.TabIndex = 0x1a;
            this.chkLogDetail.Text = "显示详细日志";
            this.chkLogDetail.UseVisualStyleBackColor = true;
            this.chkLogDetail.CheckedChanged += new EventHandler(this.chkLogDetail_CheckedChanged);
            this.groupBox4.Controls.Add(this.cboFloors);
            this.groupBox4.Controls.Add(this.btnOpenFloor);
            this.groupBox4.Location = new Point(0x127, 0x86);
            this.groupBox4.Name = "groupBox4";
            this.groupBox4.Size = new Size(0x127, 0x2b);
            this.groupBox4.TabIndex = 0x19;
            this.groupBox4.TabStop = false;
            this.groupBox4.Text = "一对多(或梯控)";
            this.cboFloors.FormattingEnabled = true;
            this.cboFloors.Items.AddRange(new object[] { "1号", "2号", "3号", "4号" });
            this.cboFloors.Location = new Point(100, 0x10);
            this.cboFloors.Name = "cboFloors";
            this.cboFloors.Size = new Size(0x36, 20);
            this.cboFloors.TabIndex = 0x19;
            this.cboFloors.Text = "1号门";
            this.btnOpenFloor.Location = new Point(15, 14);
            this.btnOpenFloor.Name = "btnOpenFloor";
            this.btnOpenFloor.Size = new Size(0x4f, 0x17);
            this.btnOpenFloor.TabIndex = 0x12;
            this.btnOpenFloor.Text = "远程开(NO)";
            this.btnOpenFloor.UseVisualStyleBackColor = true;
            this.btnOpenFloor.Click += new EventHandler(this.btnOpenFloor_Click);
            this.cboDoors.FormattingEnabled = true;
            this.cboDoors.Items.AddRange(new object[] { "1号门", "2号门", "3号门", "4号门" });
            this.cboDoors.Location = new Point(0x18b, 0x37);
            this.cboDoors.Name = "cboDoors";
            this.cboDoors.Size = new Size(0x36, 20);
            this.cboDoors.TabIndex = 0x18;
            this.cboDoors.Text = "1号门";
            this.btnControllerInfo.Location = new Point(0x206, 0x19);
            this.btnControllerInfo.Name = "btnControllerInfo";
            this.btnControllerInfo.Size = new Size(0x35, 0x17);
            this.btnControllerInfo.TabIndex = 0x17;
            this.btnControllerInfo.Text = "信息";
            this.btnControllerInfo.UseVisualStyleBackColor = true;
            this.btnControllerInfo.Click += new EventHandler(this.btnControllerInfo_Click);
            this.groupBox2.Controls.Add(this.chkAllControllerB);
            this.groupBox2.Controls.Add(this.btnUploadAllPrivileges);
            this.groupBox2.Controls.Add(this.btnUploadPrivilegeSingle);
            this.groupBox2.Controls.Add(this.txtCardNO);
            this.groupBox2.Controls.Add(this.label9);
            this.groupBox2.Controls.Add(this.dateEndHMS1);
            this.groupBox2.Controls.Add(this.dtpDeactivate);
            this.groupBox2.Controls.Add(this.dtpActivate);
            this.groupBox2.Controls.Add(this.label7);
            this.groupBox2.Controls.Add(this.label8);
            this.groupBox2.Location = new Point(0x255, 13);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new Size(0x1ad, 0x7b);
            this.groupBox2.TabIndex = 0x16;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "用户权限";
            this.chkAllControllerB.AutoSize = true;
            this.chkAllControllerB.Location = new Point(0x129, 0x10);
            this.chkAllControllerB.Name = "chkAllControllerB";
            this.chkAllControllerB.Size = new Size(0x6c, 0x10);
            this.chkAllControllerB.TabIndex = 0x22;
            this.chkAllControllerB.Text = "上传到全部设备";
            this.chkAllControllerB.UseVisualStyleBackColor = true;
            this.btnUploadAllPrivileges.Location = new Point(0x10a, 0x29);
            this.btnUploadAllPrivileges.Name = "btnUploadAllPrivileges";
            this.btnUploadAllPrivileges.Size = new Size(0x9d, 0x2c);
            this.btnUploadAllPrivileges.TabIndex = 0x21;
            this.btnUploadAllPrivileges.Text = "上传更新所有权限[1万条] 1024字节指令实现";
            this.btnUploadAllPrivileges.UseVisualStyleBackColor = true;
            this.btnUploadAllPrivileges.Click += new EventHandler(this.btnUploadAllPrivileges_Click);
            this.btnUploadPrivilegeSingle.Location = new Point(0x12, 0x5d);
            this.btnUploadPrivilegeSingle.Name = "btnUploadPrivilegeSingle";
            this.btnUploadPrivilegeSingle.Size = new Size(0xab, 0x17);
            this.btnUploadPrivilegeSingle.TabIndex = 0x20;
            this.btnUploadPrivilegeSingle.Text = "上传单个权限(单台控制器)";
            this.btnUploadPrivilegeSingle.UseVisualStyleBackColor = true;
            this.btnUploadPrivilegeSingle.Click += new EventHandler(this.btnUploadPrivilegeSingle_Click);
            this.txtCardNO.Location = new Point(70, 14);
            this.txtCardNO.Name = "txtCardNO";
            this.txtCardNO.Size = new Size(100, 0x15);
            this.txtCardNO.TabIndex = 30;
            this.txtCardNO.Text = "12345";
            this.label9.AutoSize = true;
            this.label9.Location = new Point(15, 0x11);
            this.label9.Name = "label9";
            this.label9.Size = new Size(0x1d, 12);
            this.label9.TabIndex = 0x1f;
            this.label9.Text = "卡号";
            this.dateEndHMS1.Format = DateTimePickerFormat.Time;
            this.dateEndHMS1.Location = new Point(0x95, 0x43);
            this.dateEndHMS1.Name = "dateEndHMS1";
            this.dateEndHMS1.ShowUpDown = true;
            this.dateEndHMS1.Size = new Size(0x49, 0x15);
            this.dateEndHMS1.TabIndex = 0x1a;
            this.dateEndHMS1.Value = new DateTime(0x7e0, 9, 2, 0x17, 0x3b, 0, 0);
            this.dtpDeactivate.Location = new Point(0x2b, 0x43);
            this.dtpDeactivate.Name = "dtpDeactivate";
            this.dtpDeactivate.Size = new Size(0x68, 0x15);
            this.dtpDeactivate.TabIndex = 0x19;
            this.dtpDeactivate.Value = new DateTime(0x7ed, 12, 0x1f, 0x17, 0x3b, 0, 0);
            this.dtpActivate.Location = new Point(70, 0x29);
            this.dtpActivate.Name = "dtpActivate";
            this.dtpActivate.Size = new Size(0x70, 0x15);
            this.dtpActivate.TabIndex = 0x1b;
            this.dtpActivate.Value = new DateTime(0x7da, 1, 1, 0x12, 0x12, 0, 0);
            this.label7.AutoSize = true;
            this.label7.ForeColor = Color.Black;
            this.label7.ImeMode = ImeMode.NoControl;
            this.label7.Location = new Point(14, 0x2f);
            this.label7.Name = "label7";
            this.label7.Size = new Size(0x3b, 12);
            this.label7.TabIndex = 0x1c;
            this.label7.Text = "有效期从:";
            this.label8.AutoSize = true;
            this.label8.ForeColor = Color.Black;
            this.label8.ImeMode = ImeMode.NoControl;
            this.label8.Location = new Point(14, 70);
            this.label8.Name = "label8";
            this.label8.Size = new Size(0x17, 12);
            this.label8.TabIndex = 0x1d;
            this.label8.Text = "到:";
            this.btnAdjustTime.Location = new Point(0x1c7, 0x34);
            this.btnAdjustTime.Name = "btnAdjustTime";
            this.btnAdjustTime.Size = new Size(0x74, 0x17);
            this.btnAdjustTime.TabIndex = 0x15;
            this.btnAdjustTime.Text = "校准时间";
            this.btnAdjustTime.UseVisualStyleBackColor = true;
            this.btnAdjustTime.Click += new EventHandler(this.btnAdjustTime_Click);
            this.chkAllControllers.AutoSize = true;
            this.chkAllControllers.Location = new Point(310, 0x56);
            this.chkAllControllers.Name = "chkAllControllers";
            this.chkAllControllers.Size = new Size(0x6c, 0x10);
            this.chkAllControllers.TabIndex = 20;
            this.chkAllControllers.Text = "取全部设备记录";
            this.chkAllControllers.UseVisualStyleBackColor = true;
            this.chkGetAllSwipes.AutoSize = true;
            this.chkGetAllSwipes.Location = new Point(0x1ab, 0x56);
            this.chkGetAllSwipes.Name = "chkGetAllSwipes";
            this.chkGetAllSwipes.Size = new Size(0x90, 0x10);
            this.chkGetAllSwipes.TabIndex = 0x13;
            this.chkGetAllSwipes.Text = "取所有记录(忽略标记)";
            this.chkGetAllSwipes.UseVisualStyleBackColor = true;
            this.btnGetAllSwipes.Location = new Point(310, 0x6c);
            this.btnGetAllSwipes.Name = "btnGetAllSwipes";
            this.btnGetAllSwipes.Size = new Size(0xd8, 0x17);
            this.btnGetAllSwipes.TabIndex = 0x12;
            this.btnGetAllSwipes.Text = "(1024字节指令实现 提取记录)";
            this.btnGetAllSwipes.UseVisualStyleBackColor = true;
            this.btnGetAllSwipes.Click += new EventHandler(this.btnGetAllSwipes_Click);
            this.btnRemoteOpenDoor1.Location = new Point(310, 0x36);
            this.btnRemoteOpenDoor1.Name = "btnRemoteOpenDoor1";
            this.btnRemoteOpenDoor1.Size = new Size(0x4f, 0x17);
            this.btnRemoteOpenDoor1.TabIndex = 0x11;
            this.btnRemoteOpenDoor1.Text = "远程开";
            this.btnRemoteOpenDoor1.UseVisualStyleBackColor = true;
            this.btnRemoteOpenDoor1.Click += new EventHandler(this.btnRemoteOpenDoor_Click);
            this.checkBox2.AutoSize = true;
            this.checkBox2.Checked = true;
            this.checkBox2.CheckState = CheckState.Checked;
            this.checkBox2.Location = new Point(0x22, 0x53);
            this.checkBox2.Name = "checkBox2";
            this.checkBox2.Size = new Size(150, 0x10);
            this.checkBox2.TabIndex = 0x10;
            this.checkBox2.Text = "二维码数据-->远程开门";
            this.checkBox2.UseVisualStyleBackColor = true;
            this.checkBox1.AutoSize = true;
            this.checkBox1.Checked = true;
            this.checkBox1.CheckState = CheckState.Checked;
            this.checkBox1.Location = new Point(0x22, 0x3d);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new Size(0x8a, 0x10);
            this.checkBox1.TabIndex = 15;
            this.checkBox1.Text = "刷卡记录-->远程开门";
            this.checkBox1.UseVisualStyleBackColor = true;
            this.txtSN.BackColor = Color.WhiteSmoke;
            this.txtSN.Location = new Point(0x19c, 0x19);
            this.txtSN.Name = "txtSN";
            this.txtSN.ReadOnly = true;
            this.txtSN.Size = new Size(100, 0x15);
            this.txtSN.TabIndex = 13;
            this.label1.AutoSize = true;
            this.label1.Location = new Point(0x143, 0x16);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x53, 0x18);
            this.label1.TabIndex = 14;
            this.label1.Text = "Controller SN\r\n 控制器序列号";
            this.lstControllers.Anchor = AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Top;
            this.lstControllers.FormattingEnabled = true;
            this.lstControllers.ItemHeight = 12;
            this.lstControllers.Location = new Point(0x1fa, 0x1f);
            this.lstControllers.Name = "lstControllers";
            this.lstControllers.Size = new Size(0x77, 0x184);
            this.lstControllers.TabIndex = 0x10;
            this.lstControllers.Click += new EventHandler(this.lstControllers_Click);
            this.lstSwipe.Anchor = AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Top;
            this.lstSwipe.Columns.AddRange(new ColumnHeader[] { this.columnHeader1, this.columnHeader2, this.columnHeader3, this.columnHeader4 });
            this.lstSwipe.Location = new Point(0x277, 30);
            this.lstSwipe.Name = "lstSwipe";
            this.lstSwipe.Size = new Size(0x182, 0x185);
            this.lstSwipe.TabIndex = 0x12;
            this.lstSwipe.UseCompatibleStateImageBehavior = false;
            this.lstSwipe.View = View.Details;
            this.columnHeader1.Tag = "卡号";
            this.columnHeader1.Text = "卡号";
            this.columnHeader1.Width = 100;
            this.columnHeader2.Text = "时间";
            this.columnHeader2.Width = 0xa5;
            this.columnHeader3.Text = "SN";
            this.columnHeader3.Width = 70;
            this.columnHeader4.Text = "门号";
            this.columnHeader4.Width = 0x27;
            this.label2.Anchor = AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Top;
            this.label2.AutoSize = true;
            this.label2.Location = new Point(0x275, 13);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x4d, 12);
            this.label2.TabIndex = 0x13;
            this.label2.Text = "实时刷卡记录";
            this.label4.Anchor = AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Top;
            this.label4.AutoSize = true;
            this.label4.Location = new Point(0x1fb, 13);
            this.label4.Name = "label4";
            this.label4.Size = new Size(0x59, 12);
            this.label4.TabIndex = 20;
            this.label4.Text = "已接入的控制器";
            this.label6.AutoSize = true;
            this.label6.Font = new Font("宋体", 10.5f, FontStyle.Regular, GraphicsUnit.Point, 0x86);
            this.label6.Location = new Point(0x177, 7);
            this.label6.Name = "label6";
            this.label6.Size = new Size(0x20d, 0x54);
            this.label6.TabIndex = 0x15;
            this.label6.Text = "此应用 关注点:  (控制器主动上传数据)\r\n1. 实时监控最新的刷卡记录\r\n2. 对接收到的记录分析 发出远程开门指令  \r\n3. 实时接收二维码信息, 分析后发出远程开门指令.\r\n4. 应用以正常通信为基准, [丢包率低]    对于发出的指令如果控制器没有接收到,\r\n   用户可以再刷一次卡, 这样重试达到开门效果";
            this.groupBox3.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.groupBox3.Controls.Add(this.btnClear);
            this.groupBox3.Controls.Add(this.txtInfo);
            this.groupBox3.Controls.Add(this.lstControllers);
            this.groupBox3.Controls.Add(this.label4);
            this.groupBox3.Controls.Add(this.lstSwipe);
            this.groupBox3.Controls.Add(this.label2);
            this.groupBox3.Location = new Point(0x16, 0x11c);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new Size(0x408, 430);
            this.groupBox3.TabIndex = 0x16;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "信息";
            this.btnClear.Location = new Point(0x23, 8);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new Size(0x4b, 0x17);
            this.btnClear.TabIndex = 0x15;
            this.btnClear.Text = "清空";
            this.btnClear.UseVisualStyleBackColor = true;
            this.btnClear.Click += new EventHandler(this.btnClear_Click);
            this.chkDisplayIP.AutoSize = true;
            this.chkDisplayIP.Location = new Point(0x135, 0x52);
            this.chkDisplayIP.Name = "chkDisplayIP";
            this.chkDisplayIP.Size = new Size(60, 0x10);
            this.chkDisplayIP.TabIndex = 30;
            this.chkDisplayIP.Text = "显示IP";
            this.chkDisplayIP.UseVisualStyleBackColor = true;
            this.chkDisplayIP.CheckedChanged += new EventHandler(this.chkDisplayIP_CheckedChanged);
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x42b, 0x2ca);
            base.Controls.Add(this.chkDisplayIP);
            base.Controls.Add(this.button4);
            base.Controls.Add(this.label6);
            base.Controls.Add(this.groupBox1);
            base.Controls.Add(this.txtWatchServerPort);
            base.Controls.Add(this.label3);
            base.Controls.Add(this.txtWatchServerIP);
            base.Controls.Add(this.label5);
            base.Controls.Add(this.groupBox3);
            base.Name = "FormCloudServer";
            this.Text = "服务器控制 V4.3 - 20180523";
            base.FormClosing += new FormClosingEventHandler(this.FormCloudServer_FormClosing);
            base.Load += new EventHandler(this.FormCloudServer_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox4.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void log(string info)
        {
            base.Invoke(new dlgLog(this.logEntry), new object[] { info });
        }

        private void logDetail(string info)
        {
            if (this.bDisplayDetail)
            {
                base.Invoke(new dlgLog(this.logEntry), new object[] { info });
            }
        }

        private void logEntry(string info)
        {
            if (this.txtInfo.Text.Length > 0x7d00)
            {
                int index = this.txtInfo.Text.IndexOf("\r\n", 0x3e80);
                this.txtInfo.Text = this.txtInfo.Text.Substring(index + 2);
            }
            this.txtInfo.AppendText($"{DateTime.Now.ToString("HH:mm:ss")} {info}");
            this.txtInfo.ScrollToCaret();
            Application.DoEvents();
        }

        private void LongToBytes(ref byte[] outBytes, int startIndex, long val)
        {
            Array.Copy(BitConverter.GetBytes(val), 0, outBytes, startIndex, 4);
        }

        private void lstControllers_Click(object sender, EventArgs e)
        {
            if (this.lstControllers.Items.Count > 0)
            {
                if (this.lstControllers.SelectedIndex < 0)
                {
                    this.lstControllers.SelectedIndex = 0;
                }
                this.txtSN.Text = this.lstControllers.Items[this.lstControllers.SelectedIndex].ToString();
            }
        }

        private int remoteOpenDoor(ref wgUdpServerCom serverCurrent, long sn, int doorNO, int inOrOut, long cardNO)
        {
            int num = -13;
            byte[] destinationArray = new byte[0x40];
            for (int i = 0; i < 0x40; i++)
            {
                destinationArray[i] = 0;
            }
            destinationArray[0] = 0x17;
            destinationArray[1] = 0x40;
            Array.Copy(BitConverter.GetBytes(sn), 0, destinationArray, 4, 4);
            destinationArray[8] = (byte) (doorNO & 0xff);
            destinationArray[0x1c] = (inOrOut == 1) ? ((byte) 0) : ((byte) 1);
            Array.Copy(BitConverter.GetBytes(cardNO), 0, destinationArray, 20, 4);
            Array.Copy(BitConverter.GetBytes(cardNO), 4, destinationArray, 0x18, 4);
            destinationArray[0x20] = 90;
            int num3 = 3;
            long sequenceId = 0L;
            while (num3 > 0)
            {
                serverCurrent.receivedReplyClear();
                sequenceId = serverCurrent.UDP_OnlySend(destinationArray, sequenceId);
                if (sequenceId >= 0L)
                {
                    this.log($" ====> 发出远程开门指令 控制器SN={sn.ToString()}, 门号= {doorNO.ToString()}, 进出门：{inOrOut}， 模拟卡号= {cardNO}, 流水号= 0x{sequenceId:X} ");
                }
                else
                {
                    this.log($"====>??? 失败: 发出远程开门指令 控制器SN={sn.ToString()}, 门号= {doorNO.ToString()}, 进出门：{inOrOut}， 模拟卡号= {cardNO} ");
                    this.log("控制器 没有连接...");
                    return num;
                }
                int num5 = 100;
                while (num5-- > 0)
                {
                    if (this.udpserverLast.receivedReplyCount() > 0)
                    {
                        byte[] buff = this.udpserverLast.getReply();
                        if (this.byteToLong(buff, 40, 4) != sequenceId)
                        {
                            continue;
                        }
                        this.log($"====>收到返回 信息 {BitConverter.ToString(buff)} ");
                        this.log((buff[8] == 1) ? "成功!\r\n" : "失败...\r\n");
                        if (buff[8] == 1)
                        {
                            num = 1;
                        }
                        break;
                    }
                    Thread.Sleep(10);
                }
                if ((num5 >= 0) || (num >= 0))
                {
                    break;
                }
                num3--;
            }
            if (num <= 0)
            {
                this.log("失败...\r\n");
            }
            return num;
        }

        private int remoteOpenFloor(ref wgUdpServerCom serverCurrent, long sn, int floorNO, long cardNO)
        {
            int num = -13;
            byte[] destinationArray = new byte[0x40];
            for (int i = 0; i < 0x40; i++)
            {
                destinationArray[i] = 0;
            }
            destinationArray[0] = 0x17;
            destinationArray[1] = 0x40;
            Array.Copy(BitConverter.GetBytes(sn), 0, destinationArray, 4, 4);
            int num3 = 1;
            destinationArray[8] = (byte) (num3 & 0xff);
            destinationArray[9] = (byte) (floorNO & 0xff);
            Array.Copy(BitConverter.GetBytes(cardNO), 0, destinationArray, 20, 4);
            Array.Copy(BitConverter.GetBytes(cardNO), 4, destinationArray, 0x18, 4);
            destinationArray[0x20] = 90;
            int num4 = 3;
            long sequenceId = 0L;
            while (num4 > 0)
            {
                serverCurrent.receivedReplyClear();
                sequenceId = serverCurrent.UDP_OnlySend(destinationArray, sequenceId);
                if (sequenceId >= 0L)
                {
                    this.log($" ====> 发出远程开门指令 控制器SN={sn.ToString()}, 楼号= {floorNO.ToString()}  模拟卡号= {cardNO}, 流水号= 0x{sequenceId:X} ");
                }
                else
                {
                    this.log($"====>??? 失败: 发出远程开门指令 控制器SN={sn.ToString()}, 楼号= {floorNO.ToString()}  模拟卡号= {cardNO} ");
                    this.log("控制器 没有连接...");
                    return num;
                }
                int num6 = 100;
                while (num6-- > 0)
                {
                    if (this.udpserverLast.receivedReplyCount() > 0)
                    {
                        byte[] buff = this.udpserverLast.getReply();
                        if (this.byteToLong(buff, 40, 4) != sequenceId)
                        {
                            continue;
                        }
                        this.log($"====>收到返回 信息 {BitConverter.ToString(buff)} ");
                        this.log((buff[8] == 1) ? "成功!\r\n" : "失败...\r\n");
                        if (buff[8] == 1)
                        {
                            num = 1;
                        }
                        break;
                    }
                    Thread.Sleep(10);
                }
                if ((num6 >= 0) || (num >= 0))
                {
                    break;
                }
                num4--;
            }
            if (num <= 0)
            {
                this.log("失败...\r\n");
            }
            return num;
        }

        private int runComm(ref wgUdpServerCom serverCurrent, long sn, byte[] buff, ref byte[] recv)
        {
            int num = -13;
            buff[0] = 0x17;
            Array.Copy(BitConverter.GetBytes(sn), 0, buff, 4, 4);
            int num2 = 3;
            long sequenceId = 0L;
            while (num2 > 0)
            {
                serverCurrent.receivedReplyClear();
                sequenceId = serverCurrent.UDP_OnlySend(buff, sequenceId);
                if (sequenceId < 0L)
                {
                    this.log("控制器 没有连接...");
                    return num;
                }
                int num4 = 100;
                while (num4-- > 0)
                {
                    if (this.udpserverLast.receivedReplyCount() > 0)
                    {
                        byte[] buffer = this.udpserverLast.getReply();
                        if (this.byteToLong(buffer, 40, 4) != sequenceId)
                        {
                            continue;
                        }
                        recv = buffer;
                        num = 1;
                        break;
                    }
                    Thread.Sleep(10);
                }
                if ((num4 >= 0) || (num >= 0))
                {
                    break;
                }
                num2--;
            }
            if (num <= 0)
            {
                this.log("失败...\r\n");
                return num;
            }
            return (int) sequenceId;
        }

        private int sendCust64Byte(ref wgUdpServerCom serverCurrent, long sn, byte[] buff)
        {
            int num = -13;
            int num2 = 3;
            long sequenceId = 0L;
            while (num2 > 0)
            {
                serverCurrent.receivedReplyClear();
                sequenceId = serverCurrent.UDP_OnlySend(buff, sequenceId);
                if (sequenceId >= 0L)
                {
                    this.log($"====> 发出指令 控制器SN={sn.ToString()},  流水号= 0x{sequenceId:X} ");
                }
                else
                {
                    this.log($"====>??? 失败: 发出远程开门指令 控制器SN={sn.ToString()} ");
                    this.log("控制器 没有连接...");
                    return num;
                }
                int num4 = 100;
                while (num4-- > 0)
                {
                    if (this.udpserverLast.receivedReplyCount() > 0)
                    {
                        byte[] buffer = this.udpserverLast.getReply();
                        if (this.byteToLong(buffer, 40, 4) != sequenceId)
                        {
                            continue;
                        }
                        this.log($"====>收到返回 信息 {BitConverter.ToString(buffer)} ");
                        this.log((buffer[8] == 1) ? "成功!\r\n" : "失败...\r\n");
                        if (buffer[8] == 1)
                        {
                            num = 1;
                        }
                        break;
                    }
                    Thread.Sleep(10);
                }
                if ((num4 >= 0) || (num >= 0))
                {
                    break;
                }
                num2--;
            }
            if (num <= 0)
            {
                this.log("失败...\r\n");
            }
            return num;
        }

        private static int strToByteArr(string str, ref byte[] bytArr, int len)
        {
            int num = -1;
            try
            {
                int length = len;
                if (length < ((str.Length + 1) >> 1))
                {
                    length = (str.Length + 1) >> 1;
                }
                if (length < bytArr.Length)
                {
                    length = bytArr.Length;
                }
                if (length <= 0)
                {
                    return num;
                }
                for (int i = 0; i < length; i++)
                {
                    bytArr[i] = byte.Parse(str.Substring(2 * i, 2), NumberStyles.AllowHexSpecifier);
                }
                num = 0;
            }
            catch (Exception)
            {
            }
            return num;
        }

        private void uploadAllPrivlieges(object arg)
        {
            object[] objArray = arg as object[];
            wgUdpServerCom serverCurrent = (wgUdpServerCom) objArray[0];
            ArrayList list = (ArrayList) objArray[1];
            for (int i = 0; i < list.Count; i++)
            {
                long num2 = (long) list[i];
                int num3 = 0;
                int num4 = 0;
                byte[] destinationArray = new byte[0x400];
                this.log("控制器SN = " + num2.ToString());
                this.log("上传权限操作\t 开始...[1024字节指令]");
                byte[] buffer2 = new byte[0x40];
                for (int j = 0; j < 0x40; j++)
                {
                    buffer2[j] = 0;
                }
                this.log("1.21\t权限按从小到大顺序添加[功能号: 0x56]\t开始...[采用1024字节指令, 每次上传16个权限]");
                int num6 = 0x2710;
                this.log($"       {num6 / 0x2710}万条权限...");
                long[] numArray = new long[num6];
                for (int k = 0; k < num6; k++)
                {
                    numArray[k] = 0xc351 + k;
                }
                long num8 = 0L;
                int index = 0;
                while (index < num6)
                {
                    for (int m = 0; m < 0x400; m++)
                    {
                        destinationArray[m] = 0;
                    }
                    buffer2[0] = 0x17;
                    buffer2[1] = 0x56;
                    buffer2[2] = 0;
                    buffer2[3] = 0;
                    Array.Copy(BitConverter.GetBytes(num2), 0, buffer2, 4, 4);
                    for (int n = 0; n < 0x400; n += 0x40)
                    {
                        if (index >= num6)
                        {
                            break;
                        }
                        long val = numArray[index];
                        if (num8 >= val)
                        {
                            this.log("卡号没有从小到大排序...???  (上传失败)");
                            num4 = 0;
                            break;
                        }
                        num8 = val;
                        this.LongToBytes(ref buffer2, 8, val);
                        buffer2[12] = 0x20;
                        buffer2[13] = 0x10;
                        buffer2[14] = 1;
                        buffer2[15] = 1;
                        buffer2[0x10] = 0x20;
                        buffer2[0x11] = 0x29;
                        buffer2[0x12] = 0x12;
                        buffer2[0x13] = 0x31;
                        buffer2[0x26] = 0x23;
                        buffer2[0x27] = 0x59;
                        buffer2[20] = 1;
                        buffer2[0x15] = 1;
                        buffer2[0x16] = 1;
                        buffer2[0x17] = 1;
                        this.LongToBytes(ref buffer2, 0x20, (long) num6);
                        this.LongToBytes(ref buffer2, 0x23, (long) (index + 1));
                        Array.Copy(buffer2, 0, destinationArray, n, 0x40);
                        index++;
                    }
                    byte[] recv = null;
                    num3 = this.runComm(ref serverCurrent, num2, destinationArray, ref recv);
                    num4 = 0;
                    if (num3 <= 0)
                    {
                        break;
                    }
                    if (recv[8] == 1)
                    {
                        num4 = 1;
                    }
                    if (recv[8] == 0xe1)
                    {
                        this.log("1.21\t权限按从小到大顺序添加[功能号: 0x56]\t =0xE1 表示卡号没有从小到大排序...???");
                        num4 = 0;
                        break;
                    }
                }
                if (num4 == 1)
                {
                    this.log("1.21\t权限按从小到大顺序添加[功能号: 0x56]\t 成功...");
                }
                else
                {
                    this.log("1.21\t权限按从小到大顺序添加[功能号: 0x56]\t 失败...????");
                }
            }
        }

        private int uploadPrivilegSingle(ref wgUdpServerCom serverCurrent, long sn, long cardNO, DateTime beginDate, DateTime endDate)
        {
            int num = -13;
            byte[] destinationArray = new byte[0x40];
            for (int i = 0; i < 0x40; i++)
            {
                destinationArray[i] = 0;
            }
            destinationArray[0] = 0x17;
            destinationArray[1] = 80;
            Array.Copy(BitConverter.GetBytes(sn), 0, destinationArray, 4, 4);
            Array.Copy(BitConverter.GetBytes(cardNO), 0, destinationArray, 8, 4);
            Array.Copy(BitConverter.GetBytes(cardNO), 4, destinationArray, 0x2c, 4);
            DateTime time = beginDate;
            destinationArray[12] = (byte) this.GetHex((time.Year - (time.Year % 100)) / 100);
            destinationArray[13] = (byte) this.GetHex(time.Year % 100);
            destinationArray[14] = (byte) this.GetHex(time.Month);
            destinationArray[15] = (byte) this.GetHex(time.Day);
            time = endDate;
            destinationArray[0x10] = (byte) this.GetHex((time.Year - (time.Year % 100)) / 100);
            destinationArray[0x11] = (byte) this.GetHex(time.Year % 100);
            destinationArray[0x12] = (byte) this.GetHex(time.Month);
            destinationArray[0x13] = (byte) this.GetHex(time.Day);
            destinationArray[0x26] = (byte) this.GetHex(time.Hour);
            destinationArray[0x27] = (byte) this.GetHex(time.Minute);
            destinationArray[20] = 1;
            destinationArray[0x15] = 1;
            destinationArray[0x16] = 1;
            destinationArray[0x17] = 1;
            int num3 = 3;
            long sequenceId = 0L;
            while (num3 > 0)
            {
                serverCurrent.receivedReplyClear();
                sequenceId = serverCurrent.UDP_OnlySend(destinationArray, sequenceId);
                if (sequenceId >= 0L)
                {
                    this.log($"====> 发出 上传单个权限 指令 控制器SN={sn.ToString()},  卡号= {cardNO}, 流水号= 0x{sequenceId:X} ");
                }
                else
                {
                    this.log($"====>??? 失败: 发出指令 控制器SN={sn.ToString()},  卡号= {cardNO} ");
                    this.log("控制器 没有连接...");
                    return num;
                }
                int num5 = 100;
                while (num5-- > 0)
                {
                    if (this.udpserverLast.receivedReplyCount() > 0)
                    {
                        byte[] buff = this.udpserverLast.getReply();
                        if (this.byteToLong(buff, 40, 4) != sequenceId)
                        {
                            continue;
                        }
                        this.log($"====>收到返回 信息 {BitConverter.ToString(buff)} ");
                        this.log((buff[8] == 1) ? "成功!\r\n" : "失败...\r\n");
                        if (buff[8] == 1)
                        {
                            num = 1;
                        }
                        break;
                    }
                    Thread.Sleep(10);
                }
                if ((num5 >= 0) || (num >= 0))
                {
                    break;
                }
                num3--;
            }
            if (num <= 0)
            {
                this.log("失败...\r\n");
            }
            return num;
        }

        private int WatchingServerRuning(string watchServerIP, int watchServerPort)
        {
            try
            {
                wgUdpServerCom server = new wgUdpServerCom(watchServerIP, watchServerPort) {
                    IncludeIPInfo = this.chkDisplayIP.Checked
                };
                if (!server.IsWatching())
                {
                    this.log("进入接收服务器监控状态....失败");
                    return -1;
                }
                this.log("进入接收服务器监控状态....");
                long num = 0L;
                while (!this.bStopWatchServer)
                {
                    if (server.receivedCount() > 0)
                    {
                        byte[] buff = server.getRecords();
                        if (buff[1] == 0x20)
                        {
                            long num3 = this.byteToLong(buff, 4, 4);
                            if (wgControllerInfo.GetControllerType((int) num3) > 0)
                            {
                                this.logDetail($"接收到来自控制器SN = {num3} 的数据包..");
                                this.udpserverLast = server;
                                if (server.IncludeIPInfo && ((buff.Length % 0x40) == 4))
                                {
                                    this.logDetail($"接收到来自控制器IP = {buff[buff.Length - 4]:d}.{buff[buff.Length - 3]:d}.{buff[buff.Length - 2]:d}.{buff[buff.Length - 1]:d} 的数据包..");
                                }
                                long num4 = this.byteToLong(buff, 8, 4);
                                int index = this.arrControllerSN.IndexOf(num3);
                                if (index >= 0)
                                {
                                    num = (long) this.arrRecordIndex[index];
                                    this.arrRecordIndex[index] = num4;
                                }
                                else
                                {
                                    num = 0L;
                                    this.arrControllerSN.Add(num3);
                                    this.arrRecordIndex.Add(num4);
                                    this.lstControllers.Items.Add(num3);
                                }
                                if ((num < num4) || ((num4 - num) < -5L))
                                {
                                    num = num4;
                                    this.displayRecordInformation(buff);
                                    this.dealSwipeRecord(buff, ref server);
                                }
                            }
                        }
                        if (buff[1] == 0x22)
                        {
                            long num6 = this.byteToLong(buff, 4, 4);
                            if (wgControllerInfo.GetControllerType((int) num6) > 0)
                            {
                                this.logDetail($"接收到来自控制器SN = {num6} 的二维码数据包..");
                                this.udpserverLast = server;
                                if (server.IncludeIPInfo && ((buff.Length % 0x40) == 4))
                                {
                                    this.logDetail($"接收到来自控制器IP = {buff[buff.Length - 4]:d}.{buff[buff.Length - 3]:d}.{buff[buff.Length - 2]:d}.{buff[buff.Length - 1]:d} 的二维码数据包..");
                                }
                                this.byteToLong(buff, 8, 4);
                                this.dealQRData(buff, ref server);
                            }
                        }
                    }
                    else
                    {
                        Thread.Sleep(10);
                        Application.DoEvents();
                    }
                }
                server.Close();
                return 1;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
            return 0;
        }

        private delegate void dlgAddSwipeItems(ListViewItem info);

        private delegate void dlgLog(string info);
    }
}

